package servlet.index;


import com.google.gson.Gson;
import dao.ProductTypeDao;
import util.ProjectUitl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryTypeNameServlet", value = "/QueryTypeNameServlet.s")
public class QueryTypeNameServlet extends HttpServlet {
    private ProductTypeDao dao = new ProductTypeDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final List<Map<String, Object>> maps =dao.queryType();
        //type name
        //label tid
        final List<Map<String, Object>> data = new ArrayList<>();
        maps.forEach(map->{
            final String value = ProjectUitl.objToString(map.get("tid"));
            final String label = ProjectUitl.objToString(map.get("tname"));
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("value",value);
            dataMap.put("label",label);
            data.add(dataMap);
        });

        final String json = new Gson().toJson(data);
        response.getWriter().append(json);
    }
}
